package com.kintreda.ieco_server.module.organization.service.impl;

import com.kintreda.ieco_server.bean.account.Organization;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.organization.service.IOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.organization.service.impl
 * @Author ZhongYao_H
 * @Date 2019/4/28:17:35
 * @Year 2019 Month 04 Day 28
 * @Date_zh 2019 年 四月 星期日
 */
@Service
public class OrganizationServiceImpl extends BaseServiceImpl<Organization,String> implements IOrganizationService {

    @Autowired
    private IBaseRepository<Organization,String> organizationRepository;

    @Override
    public List<Organization> queryByChild(String current) {
        List<Organization> query = query("from Organization where  status = 0 and (id = ? OR pid = ?) ", new Object[]{current, current});
        return query;

    }

    @Override
    public Organization getByAndminId(String adminId) {
        List<Organization> query = query("from Organization where adminId = ? ", new Object[]{adminId});
        return query.size()>0?query.get(0):null;
    }

    @Override
    public Boolean isExist(String name) {
        return query("from Organization where name = ? and status = ? ",new Object[]{name,2}).size()>0;
    }

    @Override
    public Organization getTop(Integer top) {

        organizationRepository.findListBySql("select SUM(total) as top  from recyclerecord where userId in (select u.id from common_user u , organization o where  u.orgId= o.id ) ORDER BY  top desc LIMIT 0,?;",new String[]{top.toString()});

        return null;
    }

    @Override
    public Boolean isApply(String userId,Integer status) {
        List<Organization> query = query("from Organization where adminId = ? and orgStatus = ?", new Object[]{userId, status});
        return query.size()>0;
    }

    @Override
    public Organization findByCode(String code) {

        List<Organization> query = query("from Organization where code = ?", new Object[]{code});

        return query.size()>0?query.get(0):null;
    }

    @Override
    public List<Organization> getListByOrgId(String orgId) {
        return  query("from Organization where orgid = ?", new Object[]{orgId});

    }

    @Override
    public Organization getOrgByAdminIdAndOrgStatus(String adminId) {
        List<Organization> query = query("from Organization where adminId = ? and orgStatus = 2 ", new Object[]{adminId});
        return query.size()>0?query.get(0):null;
    }
}
